﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{
    private Rigidbody2D rb;

    private Animator animator;

    [Tooltip("地面检查用的碰撞器")]
    public Collider2D coll;

    // 移动速度
    [Tooltip("移动速度")]
    public float speed;

    // 跳跃的力度
    [Tooltip("跳跃的力度")]
    public float jumpForce;

    [Tooltip("连跳的次数")]
    public int jumpNum = 2;
    private int jumps = 0;

    // 地面检查的层
    [Tooltip("地面所在的层")]
    public LayerMask ground;

    [Tooltip("用于最后一关的方块检测")]
    public LayerMask box;

    [Tooltip("Cherry计数的UI层")]
    public Text cherryText;

    public int cherry = 0;

    private bool isHurt;

    // 音效
    private AudioSource playSource;
    public AudioClip jumpClip;
    public AudioClip hitClip;

    // Joystick
    //public Joystick joystick;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();

        // Audio Source
        playSource = GetComponent<AudioSource>();
    }

    private void Update()
    {
        // 动画切换
        SwitchAnim();

        if (!isHurt)
        {
            Movement();
            Jump();
        }
    }

    // 物体收集
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Collctions collctions = collision.gameObject.GetComponent<Collctions>();

        if (collision.transform.tag == "Collctions")
        {
            //collctions.Death();
            cherry += 1;
            cherryText.text = cherry.ToString();
        }
    }

    // 消灭敌人
    private void OnCollisionEnter2D(Collision2D collision)
    {
        bool fallingState = animator.GetBool("falling");

        // 获取碰撞物体的坐标
        Transform collTrans = collision.gameObject.transform;

        // 获取碰撞到的物体
        Enemy enemy = collision.gameObject.GetComponent<Enemy>();

        if(collision.gameObject.tag == "Enemy")
        {
            if(fallingState && transform.position.y > collTrans.position.y)
            {
                // 触发Death动画
                enemy.DeathOn();

                // 消灭敌人产生碰撞效果
                rb.velocity = new Vector2(rb.velocity.x, jumpForce);
                animator.SetBool("jumping", true);
            }
            else if(transform.position.x < collision.gameObject.transform.position.x)
            {
                animator.SetBool("hurt", true);

                // 播放 hitClip
                playSource.clip = hitClip;
                playSource.Play();

                rb.velocity = new Vector2(-6, rb.velocity.y);
                isHurt = true;

                // 改变敌人运动方向
                enemy.SwitchDirction();
            }
            else if (transform.position.x > collision.gameObject.transform.position.x)
            {
                animator.SetBool("hurt", true);

                // 播放 hitClip
                playSource.clip = hitClip;
                playSource.Play();

                rb.velocity = new Vector2(6, rb.velocity.y);
                isHurt = true;

                // 改变敌人运动方向
                enemy.SwitchDirction();
            }
            //else if(collision.transform.tag == "Box")
            //{
            //    // 连跳次数重置
            //    jumps = 0;

            //    animator.SetBool("falling", false);
            //}
        }
    }

    // 移动
    void Movement()
    {
        // 移动输入
        //float h = Input.GetAxisRaw("Horizontal");
        float h = SimpleInput.GetAxisRaw("Horizontal");

        if(h > 0.1)
        {
            h = 1;
        }
        else if (h < -0.1)
        {
            h = -1;
        }

        animator.SetFloat("running", Mathf.Abs(h));

        rb.velocity = new Vector2(h * speed * Time.fixedDeltaTime, rb.velocity.y);

        // player的左右翻转
        //if (h != 0)
        //    transform.localScale = new Vector3(h, 1, 1);
        if (h > 0 || h == 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else if (h < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
    }

    void Jump()
    {
        // 跳跃
        if (SimpleInput.GetButtonDown("Jump") && jumps < jumpNum)
        {
            // 调用手机震动
            //Handheld.Vibrate();

            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            //rb.AddForce(new Vector2(0, jumpForce * Time.fixedDeltaTime));

            // 播放 jumpClip
            playSource.clip = jumpClip;
            playSource.Play();

            animator.SetBool("jumping", true);

            // 连跳计数
            jumps += 1;
        }
    }

    // 动画切换
    void SwitchAnim()
    {
        if(rb.velocity.y < 0)
        {
            animator.SetBool("falling", true);
        }

        if (animator.GetBool("jumping"))
        {
            if (rb.velocity.y < 0)
            {
                animator.SetBool("jumping", false);
                animator.SetBool("falling", true);
            }
        }
        else if (coll.IsTouchingLayers(ground) || coll.IsTouchingLayers(box))
        {
            // 连跳次数重置
            jumps = 0;

            animator.SetBool("falling", false);
        }
        
        if(isHurt)
        {
            animator.SetFloat("running", 0.0f);

            //Debug.Log("碰撞发生了：" +Mathf.Abs(rb.velocity.x));

            if (Mathf.Abs(rb.velocity.x) < 0.1f)
            {
                isHurt = false;
                animator.SetBool("hurt", false);
            }
        }
    }
}
